package com.dc.datacollect.conduit;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author manzhuangjie
 * 统一返回结果的类
 */

@Data
public class R {
    private Boolean success;
    private Integer code;
    private String message;
    private Object data;

    public<T> T getData(Class<T> clazz) {
        return JSON.parseObject(JSON.toJSONString(data), clazz);
    }
    // 为了隐藏普通的 getData 方法
    // private void getData() {}

    private R() {}

    /**
     * 完整成功响应自定义
     *
     * @param code    代码
     * @param message 消息
     * @return {@link R}
     */
    public static R ok(Integer code,String message) {
        R result = new R();
        result.setSuccess(true);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    public static R ok(String message) {
        ResultCode resultCode = ResultCode.SUCCESS;
        return R.ok(resultCode.getCode(),message);
    }
    public static R ok() {
        ResultCode resultCode = ResultCode.SUCCESS;
        return R.ok(resultCode.getCode(),resultCode.getMessage());
    }
    public static R ok(ResultCode resultCode) {
        return R.ok(resultCode.getCode(),resultCode.getMessage());
    }



    public static R error(Integer code,String message) {
        R result = new R();
        result.setSuccess(false);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
    public static R error(String message) {
        ResultCode resultCode = ResultCode.FAIL;
        return error(resultCode.getCode(),message);
    }
    public static R error() {
        ResultCode resultCode = ResultCode.FAIL;
        return error(resultCode.getCode(),resultCode.getMessage());
    }
    public static R error(ResultCode codeEnum) {
        return error(codeEnum.getCode(),codeEnum.getMessage());
    }

//    public static R auto(String taskName, Object object) {
//        Boolean isSuccess = true;
//        if (object == null){
//            isSuccess = false;
//        }else if (object instanceof Boolean ) {
//            isSuccess = (Boolean)object;
//        }
//        if (isSuccess) {
//            return R.ok(taskName+"成功！");
//        }
//        return R.error(taskName+"失败！");
//    }
    public static R auto(String taskName, Boolean isSuccess) {
        if (isSuccess) {
            return R.ok(taskName+"成功！");
        }
        return R.error(taskName+"失败！");
    }
    public static R auto(Boolean isSuccess) {
        return auto("",isSuccess);
    }


    public R data(Object data) {
        this.data = data;
        return this;
    }
    public R dataAsMap(Consumer<Map<String,Object>> consumer) {
        Map<String, Object> map = new HashMap<>(2);
        consumer.accept(map);
        return data(map);
    }
    public R message(String message) {
        this.message = message;
        return this;
    }

    public void response(HttpServletResponse response) {
        response.setHeader("content-type", "application/json;charset=UTF-8");
        response.setStatus(200);
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {}
        String json = JSON.toJSONString(this);
        out.print(json);
        out.flush();
        out.close();

    }
    public void response() {
        HttpServletResponse response = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
        response(response);
    }

}


